#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <sstream>
#include <iostream>
class MyData {
public:
    int id;
    std::string name;

    // 构造函数
    MyData() : id(0), name("") {}
    MyData(int _id, std::string _name) : id(_id), name(_name) {}

    // 私有成员函数，用于序列化
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version) {
        ar & id;
        ar & name;
    }
};
// 序列化
std::string serialize(const MyData& data) {
    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << data;
    return archive_stream.str();
}

// 反序列化
MyData deserialize(const std::string& serialized_data) {
    std::istringstream archive_stream(serialized_data);
    boost::archive::text_iarchive archive(archive_stream);
    MyData data;
    archive >> data;
    return data;
}

//g++ -o my_program main.cpp -lboost_serialization -lboost_iostreams
int main() {
    // 创建一个 MyData 对象
    MyData original_data(1, "Alice");

    // 序列化
    std::string serialized_data = serialize(original_data);
    std::cout << "Serialized data: " << serialized_data << std::endl;

    // 反序列化
    MyData deserialized_data = deserialize(serialized_data);
    std::cout << "Deserialized data - ID: " << deserialized_data.id
              << ", Name: " << deserialized_data.name << std::endl;

    return 0;
}